package com.wintrue.ffxs.widget.area;

import android.content.Context;
import android.text.TextUtils;

import com.wintrue.ffxs.bean.area.City;
import com.wintrue.ffxs.bean.area.County;
import com.wintrue.ffxs.bean.area.Province;
import com.wintrue.ffxs.bean.area.Street;
import com.wintrue.ffxs.config.AppConstants;

public class DefaultAddressProvider implements AddressProvider {
    private Context context;
    private String regionId = "";
    private String typeId = "0";
    private String in_type;

    public DefaultAddressProvider(Context context, String type) {
        this.context = context;
        this.in_type = type;
    }

    public AddressReceiver<Province> addressReceiver_province;
    public AddressReceiver<City> addressReceiver_city;
    public AddressReceiver<County> addressReceiver_county;
    public AddressReceiver<Street> addressReceiver_street;
    public int type = 0;
    public int shop_type = 0;


    @Override
    public void provideProvinces(final AddressReceiver<Province> addressReceiver) {
        addressReceiver_province = addressReceiver;
        if (TextUtils.equals(in_type, AppConstants.PARAM_FOR_ADDRESS_SELECT)) {
            type = 1;
            httpPostUnreadCountRequest();
        } else if (TextUtils.equals(in_type, AppConstants.PARAM_FOR_SHOPTYPE_SELECT)) {
            shop_type = 1;
            httpPostShopTYpeRequest();
        }

    }

    @Override
    public void provideCitiesWith(int provinceId, final AddressReceiver<City> addressReceiver) {
        addressReceiver_city = addressReceiver;
        if (TextUtils.equals(in_type, AppConstants.PARAM_FOR_ADDRESS_SELECT)) {
            type = 2;
            regionId = String.valueOf(provinceId);
            httpPostUnreadCountRequest();
        } else if (TextUtils.equals(in_type, AppConstants.PARAM_FOR_SHOPTYPE_SELECT)) {
            shop_type = 2;
            typeId = String.valueOf(provinceId);
            httpPostShopTYpeRequest();
        }

    }

    @Override
    public void provideCountiesWith(int cityId, final AddressReceiver<County> addressReceiver) {
        addressReceiver_county = addressReceiver;
        if (TextUtils.equals(in_type, AppConstants.PARAM_FOR_ADDRESS_SELECT)) {
            type = 3;
            regionId = String.valueOf(cityId);
            httpPostUnreadCountRequest();
        } else if (TextUtils.equals(in_type, AppConstants.PARAM_FOR_SHOPTYPE_SELECT)) {
            shop_type = 3;
            typeId = String.valueOf(cityId);
            httpPostShopTYpeRequest();
        }

    }

    @Override
    public void provideStreetsWith(int countyId, final AddressReceiver<Street> addressReceiver) {
        addressReceiver_street = addressReceiver;
        if (TextUtils.equals(in_type, AppConstants.PARAM_FOR_ADDRESS_SELECT)) {
            type = 4;
            regionId = String.valueOf(countyId);
            httpPostUnreadCountRequest();
        } else if (TextUtils.equals(in_type, AppConstants.PARAM_FOR_SHOPTYPE_SELECT)) {
            shop_type = 4;
            typeId = String.valueOf(countyId);
            httpPostShopTYpeRequest();
        }

    }


    void httpPostUnreadCountRequest() {
//        RegionTask task = new RegionTask(context,regionId);
//        task.setCallBack(false, new AbstractHttpResponseHandler<Region>() {
//
//            @Override
//            public void onSuccess(Region region) {
//                switch (type){
//                    case 1:
//                        List<Province> provinceQueryList_province = new ArrayList<>();
//                        for (int i=0;i<region.getRegionList().size();i++){
//                            Province province = new Province();
//                            province.id = Integer.valueOf(region.getRegionList().get(i).getValue());
//                            province.name = region.getRegionList().get(i).getKey();
//                            provinceQueryList_province.add(province);
//                        }
//                        addressReceiver_province.send(new ArrayList<>(provinceQueryList_province),AppConstants.PARAM_FOR_ADDRESS_SELECT);
//                        break;
//                    case 2:
//                        List<City> provinceQueryList_city = new ArrayList<>();
//                        for (int i=0;i<region.getRegionList().size();i++){
//                            City city = new City();
//                            city.id = Integer.valueOf(region.getRegionList().get(i).getValue());
//                            city.name = region.getRegionList().get(i).getKey();
//                            provinceQueryList_city.add(city);
//                        }
//                        addressReceiver_city.send(new ArrayList<>(provinceQueryList_city),AppConstants.PARAM_FOR_ADDRESS_SELECT);
//                        break;
//                    case 3:
//                        List<County> provinceQueryList_county = new ArrayList<>();
//                        for (int i=0;i<region.getRegionList().size();i++){
//                            County county = new County();
//                            county.id = Integer.valueOf(region.getRegionList().get(i).getValue());
//                            county.name = region.getRegionList().get(i).getKey();
//                            provinceQueryList_county.add(county);
//                        }
//                        addressReceiver_county.send(new ArrayList<>(provinceQueryList_county),AppConstants.PARAM_FOR_ADDRESS_SELECT);
//                        break;
//                    case 4:
//                        List<Street> provinceQueryList_street = new ArrayList<>();
//                        for (int i=0;i<region.getRegionList().size();i++){
//                            Street street = new Street();
//                            street.id = Integer.valueOf(region.getRegionList().get(i).getValue());
//                            street.name = region.getRegionList().get(i).getKey();
//                            provinceQueryList_street.add(street);
//                        }
//                        addressReceiver_street.send(new ArrayList<>(provinceQueryList_street),AppConstants.PARAM_FOR_ADDRESS_SELECT);
//                        break;
//                }
//
//            }
//
//            @Override
//            public void onFailure(String code, String error) {
//                Toast.makeText(context,error,Toast.LENGTH_SHORT).show();
////                showToastMsg(error);
//            }
//        });
//        task.send();
    }

    //店铺类型
    void httpPostShopTYpeRequest() {
//        ShopTypeTask task = new ShopTypeTask(context,typeId,String.valueOf(shop_type));
//        task.setCallBack(false, new AbstractHttpResponseHandler<ShopType>() {
//
//            @Override
//            public void onSuccess(ShopType shopType) {
//                switch (shop_type){
//                    case 1:
//                        List<Province> provinceQueryList_province = new ArrayList<>();
//                        for (int i=0;i<shopType.getTypeList().size();i++){
//                            Province province = new Province();
//                            province.id = Integer.valueOf(shopType.getTypeList().get(i).getValue());
//                            province.name = shopType.getTypeList().get(i).getKey();
//                            provinceQueryList_province.add(province);
//                        }
//                        addressReceiver_province.send(new ArrayList<>(provinceQueryList_province),AppConstants.PARAM_FOR_SHOPTYPE_SELECT);
//                        break;
//                    case 2:
//                        List<City> provinceQueryList_city = new ArrayList<>();
//                        for (int i=0;i<shopType.getTypeList().size();i++){
//                            City city = new City();
//                            city.id = Integer.valueOf(shopType.getTypeList().get(i).getValue());
//                            city.name = shopType.getTypeList().get(i).getKey();
//                            provinceQueryList_city.add(city);
//                        }
//                        addressReceiver_city.send(new ArrayList<>(provinceQueryList_city),AppConstants.PARAM_FOR_SHOPTYPE_SELECT);
//                        break;
//                    case 3:
//                        List<County> provinceQueryList_county = new ArrayList<>();
//                        for (int i=0;i<shopType.getTypeList().size();i++){
//                            County county = new County();
//                            county.id = Integer.valueOf(shopType.getTypeList().get(i).getValue());
//                            county.name = shopType.getTypeList().get(i).getKey();
//                            provinceQueryList_county.add(county);
//                        }
//                        addressReceiver_county.send(new ArrayList<>(provinceQueryList_county),AppConstants.PARAM_FOR_SHOPTYPE_SELECT);
//                        break;
//                    case 4:
//                        List<Street> provinceQueryList_street = new ArrayList<>();
//                        for (int i=0;i<shopType.getTypeList().size();i++){
//                            Street street = new Street();
//                            street.id = Integer.valueOf(shopType.getTypeList().get(i).getValue());
//                            street.name = shopType.getTypeList().get(i).getKey();
//                            provinceQueryList_street.add(street);
//                        }
//                        addressReceiver_street.send(new ArrayList<>(provinceQueryList_street),AppConstants.PARAM_FOR_SHOPTYPE_SELECT);
//                        break;
//                }
//
//            }
//
//            @Override
//            public void onFailure(String code, String error) {
//                Toast.makeText(context,error,Toast.LENGTH_SHORT).show();
//            }
//        });
//        task.send();
    }
}
